/************************* 版权声明 *********************************
 * 
 * 版权所有：Apache License 2.0
 * 
 ************************* 版本声明 *********************************
 *@since jdk1.5
 *@version 0.1
 ************************* 变更记录 *********************************
 *
 * 创建者：bijiqiang Email:bijiqiang@gmail.com   创建日期： 2009-12-17
 * 创建记录：创建类结构。
 * 
 * 修改者：       修改日期：
 * 修改记录：
 *
 * 修改者：       修改日期：
 * 修改记录：
 * ……
 ************************* To  Do *********************************
 *
 * 1、实现所有未实现的方法（抛出UnsupportedOperationException的方法）。
 * 
 ************************* 随   笔 *********************************
 *
 * 
 * 
 ******************************************************************
 */

package com.realgodo.site.common;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.opensymphony.xwork2.ActionSupport;

@Controller("ajaxFileUpload")
@Scope("prototype")
public class AjaxFileUploadAction extends ActionSupport {

	/**
	 * 
	 */
	private static final long serialVersionUID = 7838782858141112700L;

	private File file;

	@SuppressWarnings("unused")
	private String contentType;

	private String fileName;

	private String sourcepath;

	private SimpleDateFormat sdf = new SimpleDateFormat("yyyyMM");

	private Date currentDate = Calendar.getInstance().getTime();

	public String execute() throws IOException {
		return SUCCESS;
	}

	public String create() {
		if (null != fileName) {
			String filename = FileUpload.getFileName(fileName);
			String temp = Constant.UPLOAD_IMAGE_PATH + sdf.format(currentDate)
					+ "/" + filename;
			String filepath = getRequest().getSession().getServletContext()
					.getRealPath(temp);
			FileUpload.saveFile(filepath, file);
			this.sourcepath = Constant.ROOT_PATH + temp;
		}
		return SUCCESS;
	}

	/**
	 * AJAX上传加盟商图片
	 * 
	 * @return
	 */
	public String upload() {
		if (null != fileName) {
			String filename = FileUpload.getFileName(fileName);
			String temp = Constant.UPLOAD_IMAGE_PATH + sdf.format(currentDate)
					+ "/" + filename;
			String filepath = getRequest().getSession().getServletContext()
					.getRealPath(temp);
			FileUpload.saveFile(filepath, file);
			this.sourcepath = Constant.ROOT_PATH + temp;
		}
		return SUCCESS;
	}

	@Deprecated
	public String music() {
		if (null != fileName) {
			String filename = FileUpload.getFileName(fileName);
			String temp = Constant.UPLOAD_IMAGE_PATH + sdf.format(currentDate)
					+ "/" + filename;
			String filepath = getRequest().getSession().getServletContext()
					.getRealPath(temp);
			FileUpload.saveFile(filepath, file);
			this.sourcepath = Constant.DOMAIN + Constant.ROOT_PATH + temp;
		}
		return SUCCESS;
	}

	public void setUpload(File file) {
		this.file = file;
	}

	public void setUploadContentType(String contentType) {
		this.contentType = contentType;
	}

	public void setUploadFileName(String fileName) {
		this.fileName = fileName;
	}

	public String getSourcepath() {
		return sourcepath;
	}

	public void setSourcepath(String sourcepath) {
		this.sourcepath = sourcepath;
	}

	public String getFileName() {
		return fileName;
	}

	/**
	 * Convenience method to get the request
	 * 
	 * @return current request
	 */
	protected HttpServletRequest getRequest() {
		return ServletActionContext.getRequest();
	}

	/**
	 * Convenience method to get the response
	 * 
	 * @return current response
	 */
	protected HttpServletResponse getResponse() {
		return ServletActionContext.getResponse();
	}

	/**
	 * Convenience method to get the session. This will create a session if one
	 * doesn't exist.
	 * 
	 * @return the session from the request (request.getSession()).
	 */
	protected HttpSession getSession() {
		return getRequest().getSession();
	}
}
